﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Collections;
using Microsoft.SharePoint;

namespace MultiLevelSelectField
{
    /// <summary>
    /// This class holds one listitem which is selected in the MultiLevelSelectField
    /// </summary>
    public class MultiLevelSelectFieldValueItem
    {
        #region Constructors
        /// <summary>
        /// This constructor creates a item from the string
        /// </summary>
        /// <param name="itemline"></param>
        public MultiLevelSelectFieldValueItem(string itemline)
        {
            // Split each itemline into columns
            string[] columns = itemline.Split(new char[] { '\t' }, StringSplitOptions.None);
            Url = columns[0];
        }

        public MultiLevelSelectFieldValueItem(SPListItem listitem)
        {
            Url = listitem.Url;
        }
        #endregion

        /// <summary>
        /// This property holds the reference to the listitem
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// This property gets the referenced listitem
        /// </summary>
        public SPListItem ListItem
        {
            get
            {
                if (string.IsNullOrEmpty(Url))
                {
                    return null;
                }
                
                using (SPWeb web = SPContext.Current.Site.OpenWeb(Url))
                {
                    SPListItem listitem = web.GetListItem(Url);
                    return listitem;
                }
            }
        }

        public SPListItem GetListItem(SPSite site)
        {
            if (string.IsNullOrEmpty(Url))
            {
                return null;
            }

            using (SPWeb web = site.OpenWeb(Url))
            {
                SPListItem listitem = web.GetListItem(Url);
                return listitem;
            }
        }


        /// <summary>
        /// This method writes the item out to a string.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("{0}",Url);
        }
    }

    /// <summary>
    /// This class holds the selected items in the MultiLevelSelectField
    /// </summary>
    public class MultiLevelSelectFieldValue 
    {
        /// <summary>
        /// The list of selected items
        /// </summary>
        private List<MultiLevelSelectFieldValueItem> _items = new List<MultiLevelSelectFieldValueItem>();

        /// <summary>
        /// Parameterless constructor
        /// </summary>
        public MultiLevelSelectFieldValue()
        { }

        /// <summary>
        /// This constructor creates the values from a string
        /// </summary>
        /// <param name="value">The values in a string</param>
        public MultiLevelSelectFieldValue(string value)
        {
            // Split the string into itemlines
            string[] itemlines = value.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

            // Loop through each itemline
            foreach (string itemline in itemlines)
            {
                Items.Add(new MultiLevelSelectFieldValueItem(itemline));
            }
        }

        /// <summary>
        /// Returns the list of items
        /// </summary>
        public List<MultiLevelSelectFieldValueItem> Items
        {
            get
            {
                return _items;
            }
        }

        /// <summary>
        /// Writes out the items to a string
        /// </summary>
        /// <returns>A string</returns>
        public override string ToString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            foreach (MultiLevelSelectFieldValueItem item in Items)
            {
                if (sb.Length != 0) sb.Append('\n');
                sb.Append(item.ToString());
            }
            return sb.ToString();
        }

        public string ToText(SPSite site)
        {
            System.Text.StringBuilder sb = new StringBuilder();
            foreach (MultiLevelSelectFieldValueItem item in Items)
            {
                using (SPWeb web = site.OpenWeb(item.Url))
                {
                    SPListItem litem = web.GetListItem(item.Url);
                    if (sb.Length != 0) sb.Append(';');
                    sb.Append(litem.Title);
                }
            }
            return sb.ToString();
        }

        public bool IsSelected(SPListItem listitem)
        {
            foreach (MultiLevelSelectFieldValueItem valueitem in Items)
            {
                if (string.Compare(valueitem.Url, listitem.Url) == 0) return true;
            }
            return false;
        }

        public bool IsParentSelected(MultiLevelSelectField field, SPListItem listitem)
        {
            SPListItem parentlistitem = field.GetParentListItem(SPContext.Current.Site, listitem);
            while (parentlistitem!=null)
            {
                if (IsSelected(parentlistitem)) return true;
                parentlistitem = field.GetParentListItem(SPContext.Current.Site, parentlistitem);
            } 
            
            return false;
        }
    }
}
